package com.byzx.wine.realm;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import com.byzx.wine.po.Syuser;
import com.byzx.wine.service.ISyuserService;

public class MyRealm extends AuthorizingRealm {  
	
	@Autowired
	private ISyuserService userService;
    @Override  
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals){  
        SimpleAuthorizationInfo simpleAuthorInfo = new SimpleAuthorizationInfo();
        //查询数据库，取取权限标记
            simpleAuthorInfo.addStringPermission("admin:manage");
            simpleAuthorInfo.addStringPermission("user:add");
           /* simpleAuthorInfo.addStringPermission("user:update");
            simpleAuthorInfo.addStringPermission("user:del");*/
        return simpleAuthorInfo;  
    }  
    @Override  
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException { 
    	//查询数据库，取用户名密码
    	UsernamePasswordToken token =(UsernamePasswordToken) authcToken;
    	String username =token.getUsername();
    	System.out.println(userService);
    	Syuser s = userService.selectOne(username);
//    	if (token.getUsername().equals(username)) {
//			return new SimpleAuthenticationInfo(s.getName(),s.getPassword(),this.getName());
//		}else{
//			throw new AuthenticationException();  
//		}	
    	  AuthenticationInfo authcInfo = new SimpleAuthenticationInfo(s.getName(),s.getPassword(),this.getName());  
          return authcInfo;  
    }  
  
}
